package com.government.member.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.government.common.core.domain.BaseEntity;
import com.government.common.core.domain.entity.SysUser;
import com.government.common.core.domain.model.LoginUser;
import com.government.common.utils.DateUtils;
import com.government.member.domain.Vip;
import com.government.member.domain.VipInfo;
import com.government.member.domain.VipOrder;
import com.government.member.mapper.VipInfoMapper;
import com.government.member.mapper.VipMapper;
import com.government.member.mapper.VipOrderMapper;
import com.government.member.service.VipOrderService;
import com.government.member.vo.VipUpgrade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.government.common.utils.SecurityUtils.getLoginUser;

/**
 * 会员信息Service业务层处理
 *
 * @author system
 * @date 2023-09-18
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class VipOrderServiceImpl implements VipOrderService {
    @Autowired
    private VipOrderMapper vipOrderMapper;
    @Autowired
    private VipMapper vipMapper;
    @Autowired
    private VipInfoMapper vipInfoMapper;

    /**
     * 新增会员订单信息
     *
     * @param vipOrder 会员订单信息
     * @return 结果
     */
    @Override
    public int insertVipOrder(VipOrder vipOrder) {
        vipOrder.setCreateTime(DateUtils.getNowDate());
        return vipOrderMapper.insert(vipOrder);
    }


    /**
     * 新增会员订单信息
     *
     * @param vipOrder 会员订单信息
     * @return 结果
     */
    @Override
    public int updateVipOrder(VipOrder vipOrder) {
        vipOrder.setUpdateTime(DateUtils.getNowDate());
        return vipOrderMapper.updateById(vipOrder);
    }

    /**
     * 查询会员信息(根据第三方订单号)
     *
     * @param outTradeNo 第三方订单号
     * @return 结果
     */
    @Override
    public VipOrder selectVipOrderByOutTradeNo(String outTradeNo) {

        return vipOrderMapper.selectOne(Wrappers.<VipOrder>lambdaQuery().eq(VipOrder::getOutTradeNo, outTradeNo));
    }

    /**
     * 查询待支付订单(根据userid)
     *
     * @param userId
     * @return 结果
     */
    @Override
    public VipOrder selectUnpaidVipOrderByUserId(Long userId, int vipType) {

        return vipOrderMapper.selectOne(Wrappers.<VipOrder>lambdaQuery().eq(VipOrder::getUserId, userId)
                .eq(VipOrder::getOrderStatus, 0)
                .eq(VipOrder::getPayStatus, 0)
                .eq(VipOrder::getVipType, vipType)
                .eq(VipOrder::getTradeType, "native"));
    }

    @Override
    public List<VipOrder> selectOrderListByUserId(Long userId) {
        return vipOrderMapper.selectList(Wrappers.<VipOrder>lambdaQuery().eq(VipOrder::getUserId, userId).orderByDesc(BaseEntity::getCreateTime));
    }

    /**
     * 查询订单信息(根据订单号)
     *
     * @param orderId
     * @return 结果
     */
    @Override
    public VipOrder selectVipOrderById(Long orderId) {

        return vipOrderMapper.selectOne(Wrappers.<VipOrder>lambdaQuery().eq(VipOrder::getOrderId, orderId));
    }

    /**
     * 查询待支付订单
     *
     * @return
     */
    @Override
    public List<VipOrder> selectUnpaidVipOrder() {
        return vipOrderMapper.selectList(Wrappers.<VipOrder>lambdaQuery()
                .eq(VipOrder::getOrderStatus, 0)
                .eq(VipOrder::getPayStatus, 0)
        );
    }

    /**
     * 用户订单数量
     *
     * @return
     */
    @Override
    public int selectOrderCountByUserId(Long userId) {
        return vipOrderMapper.selectCount(Wrappers.<VipOrder>lambdaQuery().eq(VipOrder::getUserId, userId));
    }

//    /**
//     * 会员卡升级
//     */
//    @Override
//    public BigDecimal vipUpgrade() {
//        BigDecimal residuePrice = BigDecimal.ZERO;
//        LoginUser loginUser = getLoginUser();
//        SysUser user = loginUser.getUser();
//        List<VipOrder> orderList = vipOrderMapper.selectList(Wrappers.<VipOrder>lambdaQuery()
//                .eq(VipOrder::getUserId, user.getUserId())
//                .eq(VipOrder::getPayStatus, "2")
//                .orderByDesc(BaseEntity::getCreateTime));
//        VipOrder vipOrder = orderList.get(0);
//        Vip vip = vipMapper.selectOne(Wrappers.<Vip>lambdaQuery()
//                .eq(Vip::getUid, user.getUserId())
//                .eq(Vip::getVipType, 1)
//                .eq(Vip::getStatus, "1"));
//        if (Objects.nonNull(vip)) {
//
//            //  会员期限(天数)
//            Long vipDay = DateUtil.betweenDay(vip.getCreateTime(), vip.getMaturityTime(), true);
//            // 平摊下来每天多少钱
//            BigDecimal vipDayPrice = vipOrder.getOrderPayAmount().divide(new BigDecimal(String.valueOf(vipDay)), 2, RoundingMode.HALF_UP);
//            //  会员有效期剩余天数
//            Long residueDay = DateUtil.betweenDay(new Date(), vip.getMaturityTime(), true);
//            // 剩余价值
//            residuePrice = vipDayPrice.multiply(new BigDecimal(String.valueOf(residueDay))).setScale(2, RoundingMode.HALF_UP);
//        }
//        return residuePrice;
//    }

//    /**
//     * 会员卡升级
//     */
//    @Override
//    public VipUpgrade vipUpgrade() {
//        VipUpgrade vipUpgrade = new VipUpgrade();
//        LoginUser loginUser = getLoginUser();
//        SysUser user = loginUser.getUser();
//
//        VipInfo vipInfo = vipInfoMapper.selectById(2);
//
//        Vip vip = vipMapper.selectOne(Wrappers.<Vip>lambdaQuery()
//                .eq(Vip::getUid, user.getUserId())
//                .eq(Vip::getVipType, 1)
//                .eq(Vip::getStatus, "1"));
//        if (Objects.nonNull(vip)) {
//
//            // 平摊下来每天多少钱
//            BigDecimal vipDayPrice = vipInfo.getPrice().divide(new BigDecimal("365"), 2, RoundingMode.HALF_UP);
//            //  会员有效期剩余天数
//            Long residueDay = DateUtil.betweenDay(new Date(), vip.getMaturityTime(), true);
//            vipUpgrade.setResidueDay(residueDay);
//            vipUpgrade.setUpgradePrice(vipDayPrice.multiply(new BigDecimal(String.valueOf(residueDay))).setScale(2,RoundingMode.HALF_UP));
//            vipInfo.setPrice(vipUpgrade.getUpgradePrice());
//            vipInfo.setType(3);
//            vipInfo.setTime(residueDay.intValue());
//            vipInfo.setTimeUnit("DAY");
//            vipInfo.setName("VIP升级");
//            vipUpgrade.setVipInfo(vipInfo);
//        }
//
//        return vipUpgrade;
//    }


}
